package main;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

public class FortiNet implements Source {

	private String hostname;
	private String username;
	private String password;
	private int timeout;
	private int period;

	public FortiNet() {
	};

	public void importData(MySqlConnect mysql) {

		ShhConnector ssh = new ShhConnector(hostname, username, password,
				timeout);

		String cmd = "execute log filter view-lines 500\n execute log filter category event\n execute log display\n";
		String output = ssh.executeCmd(cmd);
		output = output.replace('\n', ' ');
		Timestamp last = mysql.getLastUpdateTime("dhcp");
		List<String> ar = Arrays.asList(output.split(": "));
		Collections.reverse(ar);
		for (ListIterator<String> i = ar.listIterator(); i.hasNext();) {
			try {
				String logEl = i.next().trim();
				if (!Character.isDigit(logEl.charAt(0)))
					continue;
				String[] logAr = logEl.split(" ");
				String[] dat = logAr[0].split("-");
				String[] tim = logAr[1].split(":");
				Timestamp t = new Timestamp(Integer.parseInt(dat[0]) - 1900,
						Integer.parseInt(dat[1]) - 1, Integer.parseInt(dat[2]),
						Integer.parseInt(tim[0]), Integer.parseInt(tim[1]),
						Integer.parseInt(tim[2]), 0);
				if (t.after(last)
						&& logAr[3].substring(5).equals("event")
						&& logAr[4].substring(8).equals("dhcp")
						&& logAr[7].substring(10, logAr[4].length() - 1)
								.equals("A")) {

					Timestamp leasedto = null;
					if (logAr[7].substring(10, logAr[4].length() - 1).equals(
							"A")) {
						Calendar c = Calendar.getInstance();
						c.setTime(t);
						c.add(Calendar.SECOND,
								Integer.parseInt(logAr[11].substring(6)));
						leasedto = new Timestamp(c.getTimeInMillis());
					}

					String host = logAr[12].substring(10,
							logAr[12].length() - 1);
					host = host.length() < 50 ? host : host.substring(0, 50);

					String[] param = { t.toString(),
							logAr[7].substring(10, logAr[4].length() - 1),
							logAr[9].substring(4),
							logAr[12].substring(10, logAr[12].length() - 1),
							logAr[10].substring(3), logAr[11].substring(6),
							logAr[3].substring(5), logAr[4].substring(8),
							logAr[8].substring(4) };
					mysql.saveDhcp(param);

				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		cmd = "execute log filter view-lines 500\n execute log filter category traffic\n execute log display\n";
		output = ssh.executeCmd(cmd);
		output = output.replace('\n', ' ');
		last = mysql.getLastUpdateTime("traffic");
		ar = Arrays.asList(output.split(": "));
		Collections.reverse(ar);
		int k = 0;
		Calendar c_min = Calendar.getInstance();
		c_min.setTime(new Date());
		for (ListIterator<String> i = ar.listIterator(); i.hasNext();) {
			try {
				String logEl = i.next().trim();
				if (!Character.isDigit(logEl.charAt(0)))
					continue;
				k++;
				String[] logAr = logEl.split(" ");
				String[] dat = logAr[0].split("-");
				String[] tim = logAr[1].split(":");
				Timestamp t = new Timestamp(Integer.parseInt(dat[0]) - 1900,
						Integer.parseInt(dat[1]) - 1, Integer.parseInt(dat[2]),
						Integer.parseInt(tim[0]), Integer.parseInt(tim[1]),
						Integer.parseInt(tim[2]), 0);
				String[] params = new String[18];
				if (t.after(last)) {
					if (logAr[3].substring(5).equals("traffic"))
						if (logAr[4].substring(8).equals("allowed")) {
							String type = logAr[3].substring(5);
							String subtype = logAr[4].substring(8);
							String src_ip = logAr[10].substring(4);
							String src_name = logAr[11].substring(8);
							int src_port = Integer.parseInt(logAr[12]
									.substring(9));
							String dst_ip = logAr[13].substring(4);
							String dst_name = logAr[14].substring(8);
							int dst_port = Integer.parseInt(logAr[15]
									.substring(9));
							String tran_ip = logAr[16].substring(8);
							int tran_port = Integer.parseInt(logAr[17]
									.substring(10));
							String service = logAr[18].substring(8);
							int proto = Integer
									.parseInt(logAr[19].substring(6));
							int duration = Integer.parseInt(logAr[21]
									.substring(9));
							int sent = Integer.parseInt(logAr[25].substring(5));
							int received = Integer.parseInt(logAr[26]
									.substring(5));
							int packet_sent = Integer.parseInt(logAr[33]
									.substring(9));
							int packet_rcvd = Integer.parseInt(logAr[34]
									.substring(9));

							params[0] = t.toString();
							params[1] = src_ip;
							params[2] = Integer.toString(duration);
							params[3] = type;
							params[4] = subtype;
							params[5] = src_name;
							params[6] = Integer.toString(src_port);
							params[7] = dst_ip;
							params[8] = dst_name;
							params[9] = Integer.toString(dst_port);
							params[10] = tran_ip;
							params[11] = Integer.toString(tran_port);
							params[12] = service;
							params[13] = Integer.toString(proto);
							params[14] = Integer.toString(sent);
							params[15] = Integer.toString(received);
							params[16] = Integer.toString(packet_sent);
							params[17] = Integer.toString(packet_rcvd);

						}
					if (logAr[4].substring(8).equals("violation")) {
						String type = logAr[3].substring(5);
						String subtype = logAr[4].substring(8);
						String src_ip = logAr[8].substring(4);
						String src_name = logAr[9].substring(8);
						int src_port = Integer.parseInt(logAr[10].substring(9));
						String dst_ip = logAr[11].substring(4);
						String dst_name = logAr[12].substring(8);
						int dst_port = Integer.parseInt(logAr[13].substring(9));
						String tran_ip = "";
						int tran_port = -1;
						String service = logAr[14].substring(8);
						int proto = Integer.parseInt(logAr[15].substring(6));
						int duration = Integer.parseInt(logAr[17].substring(9));
						int sent = Integer.parseInt(logAr[21].substring(5));
						int received = Integer.parseInt(logAr[22].substring(5));
						int packet_sent = 0;
						int packet_rcvd = 0;

						params[0] = t.toString();
						params[1] = src_ip;
						params[2] = Integer.toString(duration);
						params[3] = type;
						params[4] = subtype;
						params[5] = src_name;
						params[6] = Integer.toString(src_port);
						params[7] = dst_ip;
						params[8] = dst_name;
						params[9] = Integer.toString(dst_port);
						params[10] = tran_ip;
						params[11] = Integer.toString(tran_port);
						params[12] = service;
						params[13] = Integer.toString(proto);
						params[14] = Integer.toString(sent);
						params[15] = Integer.toString(received);
						params[16] = Integer.toString(packet_sent);
						params[17] = Integer.toString(packet_rcvd);

					}

					mysql.saveTraffic(params);
					mysql.agregateGlobal(period, params);
					mysql.agregateUser(period, params);
					
					mysql.aggregateGlobalNoPeriod(period, params);
					mysql.aggregateUserNoPeriod(period, params);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	@Override
	public void setParameters(String[] a, int aggregate_period) {
		// TODO Auto-generated method stub
		this.hostname = a[0];
		this.username = a[1];
		this.password = a[2];
		this.timeout = Integer.parseInt(a[3]);
		this.period = aggregate_period;

	}

	static ArrayList<Integer> applyUniqueKey(ArrayList<Integer> a1) {
		ArrayList<Integer> a2 = new ArrayList<Integer>();
		for (int i = 0; i < a1.size(); i++) {
			if (!a2.contains(a1.get(i))) {
				a2.add(a1.get(i));
			}
		}
		return a2;
	}

}
